/**
 * 
 */
package com.clouddong.exterapi.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * <p>Title:UnZipUtils </p>
 * <p>Description: </p>
 * <p>Company: </p> 
 * @author wyx
 * @date 2018年7月10日 下午2:15:13
*/
public class UnZipUtils {
	public static void main(String[] args) {
		try {
			boolean b=unzip("20180709020421-0.zip", "D://apache-tomcat-8.5.31/webapps/CloudDongInterface/upload/");
			System.out.println(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static synchronized boolean unzip(String zipFileName, String extPlace)
            throws Exception {
       return unZipFiles(zipFileName,extPlace);
    }
    /**
     * 解压zip格式的压缩文件到指定位置
     * 
     * @param zipFileName 压缩文件
     * @param extPlace 解压目录
     * @throws Exception
     */
    public static boolean unZipFiles(String zipFileName, String extPlace)
            throws Exception {
        // System.setProperty("sun.zip.encoding",
        // System.getProperty("sun.jnu.encoding"));
        try {
            (new File(extPlace)).mkdirs();
            File f = new File(extPlace + zipFileName);
            @SuppressWarnings("resource")
            ZipFile zipFile = new ZipFile(extPlace + zipFileName); // 注意一定要传入绝对路径否则解压不了
            if ((!f.exists()) && (f.length() <= 0)) {
                throw new Exception("要解压的文件不存在!");
            }
            String strPath, gbkPath, strtemp;
            File tempFile = new File(extPlace);
            System.out.println("tempFile is Exist 新建解压文件" + tempFile + ": "
                    + tempFile.exists());
            strPath = tempFile.getAbsolutePath();
            Enumeration<?> e = zipFile.entries();
            while (e.hasMoreElements()) {
                ZipEntry zipEnt = (ZipEntry) e.nextElement();
                gbkPath = zipEnt.getName();
                if (zipEnt.isDirectory()) {
                    strtemp = strPath + File.separator + gbkPath;
                    File dir = new File(strtemp);
                    dir.mkdirs();
                    continue;
                } else {
                    // 读写文件
                    InputStream is = zipFile.getInputStream(zipEnt);
                    BufferedInputStream bis = new BufferedInputStream(is);
                    gbkPath = zipEnt.getName();
                    strtemp = strPath + File.separator + gbkPath;
 
                    // 建目录
                    String strsubdir = gbkPath;
                    for (int i = 0; i < strsubdir.length(); i++) {
                        if (strsubdir.substring(i, i + 1)
                                .equalsIgnoreCase("/")) {
                            String temp = strPath + File.separator
                                    + strsubdir.substring(0, i);
                            File subdir = new File(temp);
                            if (!subdir.exists())
                                subdir.mkdir();
                        }
                    }
                    FileOutputStream fos = new FileOutputStream(strtemp);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    int c;
                    while ((c = bis.read()) != -1) {
                        bos.write((byte) c);
                    }
                    bos.close();
                    fos.close();
                }
            }
            System.out.println("tempFile is Exist unzip success !");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("tempFile is Exist unzip fail !");
            return false;
        }
    }

}
